package com.group.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.group.admin.entity.ShopUserEntity;
import com.group.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;

import com.group.admin.dao.ShopHeadDao;
import com.group.admin.entity.ShopHeadEntity;
import com.group.admin.service.ShopHeadService;

import javax.servlet.http.HttpSession;


@Service("shopHeadService")
public class ShopHeadServiceImpl extends ServiceImpl<ShopHeadDao, ShopHeadEntity> implements ShopHeadService {
    
    @Autowired
    ShopHeadService shopHeadService;



    @Override
    public ShopHeadEntity getInfoByheadUserId(HttpSession session) {
        /*
        根据session获取当前登录信息
         */
        Object userlogin = session.getAttribute("loginUser");
        String s = JSON.toJSONString(userlogin);
        ShopUserEntity shopUserEntity = JSON.parseObject(s, new TypeReference<ShopUserEntity>() {
        });
        System.out.println("输出......................."+s);
        //获取当前id
        Integer userId = shopUserEntity.getUserId();
        //根据登陆人id去查询当前团长的信息
        return baseMapper.selectOne(new QueryWrapper<ShopHeadEntity>().eq("head_user_id",userId));
    }

    @Override
    public R applicationleader(ShopHeadEntity shopHeadEntity, HttpSession session) {
        /*
        根据session获取当前登录信息
        */
        Object userlogin = session.getAttribute("loginUser");
        String s = JSON.toJSONString(userlogin);
        ShopUserEntity shopUserEntity = JSON.parseObject(s, new TypeReference<ShopUserEntity>() {
        });
        System.out.println("输出......................."+s);
        //获取登录人姓名
        String userName = shopUserEntity.getUserName();
        //获取登录人头像
        String userImg = shopUserEntity.getUserImg();
        //获取登录人id
        Integer userId = shopUserEntity.getUserId();
        System.out.println("获取登录人id:"+userId);
        ShopHeadEntity headuserId = baseMapper.selectOne(new QueryWrapper<ShopHeadEntity>().eq("head_user_id",userId));
        Integer headUserId = headuserId.getHeadUserId();
        System.out.println("获取登录人id22222222:"+headUserId);
        if (headUserId.equals(userId))return R.error("此用户已经注册过了，无需重复注册");
        //插入登录人id
        shopHeadEntity.setHeadUserId(userId);
        //插入登录人头像
        shopHeadEntity.setHeadImg(userImg);
        //插入登录人名称
        shopHeadEntity.setHeadUserName(userName);
        baseMapper.insert(shopHeadEntity);
        return R.ok("申请成功");
    }

}